import 'package:intl/intl.dart';
import 'package:umeng_push_demo/controller/app_controller.dart';
import 'package:get/get.dart';

/// FileName push_message_model
///
/// @Author 陈晓星
/// @Date 2024/10/28 11:09
///
/// @Description 推送消息对象

enum PushMessageType {
  normal(0),
  custom(1),
  ;
  final int value;
  const PushMessageType(this.value);

  static PushMessageType fromValue(int value) {
    return value == 1 ? PushMessageType.custom : PushMessageType.normal;
  }
}

class PushMessageModel {
  int id = 0;
  PushMessageType type = PushMessageType.normal;
  String deviceToken = '';
  String tag = '';
  String alias = '';
  String content = '';
  String dateTime = '';

  PushMessageModel();

  static final DateFormat _format = DateFormat('yyyy-MM-dd HH:mm:ss.SSS');

  /// 从数据库中构建实例
  factory PushMessageModel.fromDB(Map<String, dynamic> map) {
    final model = PushMessageModel();
    model.id = map['id'] ?? 0;
    model.type = PushMessageType.fromValue(map['type'] ?? 0);
    model.deviceToken = map['deviceToken'] ?? "";
    model.tag = map['tag'] ?? "";
    model.alias = map['alias'] ?? "";
    model.content = map['content'] ?? "";
    model.dateTime = map['dateTime'] ?? "";
    return model;
  }

  /// 从友盟推送消息中构建实例
  factory PushMessageModel.modelFromReceiveUmengPush(String msg, bool isCustom) {
    AppController appController = Get.put(AppController());

    final model = PushMessageModel();
    model.deviceToken = appController.deviceToken.value;
    model.tag = appController.tag.value;
    model.alias = appController.alias.value;
    model.dateTime = _format.format(DateTime.now());
    model.content = msg;
    model.type = isCustom ? PushMessageType.custom : PushMessageType.normal;
    return model;
  }
}